package com.moli.iov.protocol.model;


import java.io.Serializable;
import java.util.Arrays;

import com.moli.iov.protocol.codec.DataDecoder;
import com.moli.iov.protocol.codec.Serialize;
import com.moli.iov.protocol.enums.DateTimeTag;
import com.moli.iov.protocol.enums.SamplingStateTag;
import com.moli.iov.util.ByteArrayUtil;


/**
 * 参数查询 上行报文实体
 * 
 * @ClassName ParamQueryReq
 * @author liweisong
 * @Date 2017年5月24日 上午9:07:15
 */
@Serialize(unit = "byte", isBigEndian = true)
public class ParamQueryReq extends Header implements Serializable {

    /** 
     */
    private static final long serialVersionUID = 1L;
    /**
     * 参数查询时间
     */
    @Serialize(unit = "byte", offset = header_offset + 0, size = 6)
    public DateTimeTag queryDate;

    /**
     * 参数总数N
     */
    @Serialize(unit = "byte", offset = header_offset + 6, size = 1, min = 0, max = 252)
    public short paramSum;

    /**
     * 参数项列表
     */
    public byte[] paramIdValueList;

    @Serialize(unit = "byte", offset = header_offset + 7, size = -1)
    public void setParamIdValueList(byte[] paramIdValueList) throws Exception {
        byte[] bytes = paramIdValueList;
        this.paramValueVo = new ParamValueVo();
        while (bytes.length > 1) {
            byte infoType = bytes[0];
            bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
            switch (ByteArrayUtil.getUnsignedByte(infoType)) {
            case 1:// 车载终端本地存储时间周期
                byte[] tBytes1 = Arrays.copyOfRange(bytes, 0, 2);
                paramValueVo.setTerminalStroageCycle(ByteArrayUtil.toInt(tBytes1));
                bytes = Arrays.copyOfRange(bytes, 2, bytes.length);
                break;
            case 2:// 正常时，信息上报时间间隔
                byte[] tBytes2 = Arrays.copyOfRange(bytes, 0, 2);
                paramValueVo.setInfomationReportCycle(ByteArrayUtil.toInt(tBytes2));
                bytes = Arrays.copyOfRange(bytes, 2, bytes.length);
                break;
            case 3:// 出现报警时，信息上报时间周期
                byte[] tBytes3 = Arrays.copyOfRange(bytes, 0, 2);
                paramValueVo.setAlarmReportCycle(ByteArrayUtil.toInt(tBytes3));
                bytes = Arrays.copyOfRange(bytes, 2, bytes.length);
                break;
            case 4:// 远程服务与管理平台域名长度、远程服务与管理平台域名（域名存在时长度必须有值）
                byte[] tBytes4 = Arrays.copyOfRange(bytes, 0, 1);
                int remoteServiceLen = ByteArrayUtil.toInt(tBytes4);
                paramValueVo.setRemoteServiceDomainLen(remoteServiceLen);
                bytes = Arrays.copyOfRange(bytes, 2, bytes.length);//截取掉头两个字节，一个是长度字节，一个是参数ID=5
                byte[] tBytes5 = Arrays.copyOfRange(bytes, 0, remoteServiceLen);
                paramValueVo.setRemoteServiceDomainName(ByteArrayUtil.asciiToStr(tBytes5));
                bytes = Arrays.copyOfRange(bytes, remoteServiceLen, bytes.length);
                break;
            case 6:// 远程服务与管理平台端口
                byte[] tBytes6 = Arrays.copyOfRange(bytes, 0, 2);
                paramValueVo.setRemoteServiceDomainPort(ByteArrayUtil.toInt(tBytes6));
                bytes = Arrays.copyOfRange(bytes, 2, bytes.length);
                break;
            case 7:// 硬件版本，车载终端厂商自行定义
                byte[] tBytes7 = Arrays.copyOfRange(bytes, 0, 5);
                paramValueVo.setHardwortVersion(ByteArrayUtil.asciiToStrSplit(tBytes7));
                bytes = Arrays.copyOfRange(bytes, 5, bytes.length);
                break;
            case 8:// 固件版本，车载终端厂商自行定义
                byte[] tBytes8 = Arrays.copyOfRange(bytes, 0, 5);
                paramValueVo.setFirmwareVersion(ByteArrayUtil.asciiToStrSplit(tBytes8));
                bytes = Arrays.copyOfRange(bytes, 5, bytes.length);
                break;
            case 9:// 车载终端心跳发送周期
                byte[] tBytes9 = Arrays.copyOfRange(bytes, 0, 1);
                paramValueVo.setTerminalHeartCycle(ByteArrayUtil.toInt(tBytes9));
                bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
                break;
            case 10:// 终端应答超时时间
                byte[] tBytes10 = Arrays.copyOfRange(bytes, 0, 2);
                paramValueVo.setTerminalAnswerTimeOut(ByteArrayUtil.toInt(tBytes10));
                bytes = Arrays.copyOfRange(bytes, 2, bytes.length);
                break;
            case 11:// 平台应答超时时间
                byte[] tBytes11 = Arrays.copyOfRange(bytes, 0, 2);
                paramValueVo.setPlatformAnswerTimeOut(ByteArrayUtil.toInt(tBytes11));
                bytes = Arrays.copyOfRange(bytes, 2, bytes.length);
                break;
            case 12:// 连续三次登入失败后，到下一次登入的间隔时间
                byte[] tBytes12 = Arrays.copyOfRange(bytes, 0, 1);
                paramValueVo.setLoginOutFailInteval(ByteArrayUtil.toInt(tBytes12));
                bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
                break;
            case 13:// 公共平台域名长度N、公共平台域名（存在域名必须存在长度）
                byte[] tBytes13 = Arrays.copyOfRange(bytes, 0, 1);
                int publicLen = ByteArrayUtil.toInt(tBytes13);//域名长度N
                paramValueVo.setPublicPlatformDomainLen(publicLen);
                bytes = Arrays.copyOfRange(bytes, 2, bytes.length);//截取掉头两个字节，一个是长度字节，一个是参数ID=14
                byte[] tBytes14 = Arrays.copyOfRange(bytes, 0, publicLen);
                paramValueVo.setPublicPlatformDomainName(ByteArrayUtil.asciiToStr(tBytes14));
                bytes = Arrays.copyOfRange(bytes, publicLen, bytes.length);
                break;
            case 15:// 公共平台端口
                byte[] tBytes15 = Arrays.copyOfRange(bytes, 0, 2);
                paramValueVo.setPublicPlatformDomainPort(ByteArrayUtil.toInt(tBytes15));
                bytes = Arrays.copyOfRange(bytes, 2, bytes.length);
                break;
            case 16:// 是否处于抽样监测
                byte[] tBytes16 = Arrays.copyOfRange(bytes, 0, 1);
                paramValueVo.setIsMonitor(SamplingStateTag.valOf(tBytes16[0]));
                bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
                break;
            default:
                break;
            }
        }
    }

    public ParamValueVo paramValueVo;

    public byte[] getParamIdValueList() {
        return paramIdValueList;
    }

    public DateTimeTag getQueryDate() {
        return queryDate;
    }

    public void setQueryDate(DateTimeTag queryDate) {
        this.queryDate = queryDate;
    }

    public short getParamSum() {
        return paramSum;
    }

    public void setParamSum(short paramSum) {
        this.paramSum = paramSum;
    }

    public ParamValueVo getParamValueVo() {
        return paramValueVo;
    }

    public void setParamValueVo(ParamValueVo paramValueVo) {
        this.paramValueVo = paramValueVo;
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ParamQueryReq对象-->{");
        builder.append("\r\n");
        builder.append("时间=" + queryDate);
        builder.append("\r\n");
        builder.append("参数总数=");
        builder.append(paramSum);
        builder.append("\r\n");
        builder.append("paramValueVo=");
        builder.append(paramValueVo);
        builder.append("\r\n");
        builder.append("}");
        return builder.toString();
    }

    public static void main(String[] args) {
        byte[] bytes = new byte[] {0x23, 0x23, (byte) 0x80, 0x01, 0x4d, 0x4d, 0x56, 0x32, 0x41, 0x52, 0x42, 0x32, 0x35,
                0x47, 0x30, 0x30, 0x31, 0x30, 0x39, 0x33, 0x36, 0x01, 0x00, 0x0a, 0x11, 0x05, 0x18, 0x0d, 0x34, 0x06,
                0x10, 
                0x01, 0x27, 0x10,
                0x02, 0x01, (byte) 0xf4, 
                0x03, 0x02, 0x58, 
                0x04, 0x12,
                0x05, 0x73, 0x79, 0x73, 0x75, 0x75, 0x6d, 0x2e, 0x77, 0x2d, 0x6f, 0x61, 0x73, 0x69, 0x73, 0x2e, 0x63, 0x6f, 0x6d,
                0x06, 0x1f, (byte)0x90,
                0x07, 0x00, 0x00, 0x03, 0x01, 0x00,
                0x08, 0x00, 0x00, 0x01, 0x1e, 0x32,
                0x09, (byte)0xc8,
                0x0a, 0x27, 0x10,
                0x0b, 0x01, (byte) 0xf4, 
                0x0c, (byte)0xc8,
                0x0d, 0x13,
                0x0e, 0x64, 0x65, 0x76, 0x69, 0x6f, 0x76, 0x6e, 0x2e, 0x77, 0x2d, 0x6f, 0x61, 0x73, 0x69, 0x73, 0x2e, 0x63, 0x6f, 0x6d,
                0x0f, 0x23, 0x28,
                0x10, 0x01,
                (byte)0xf1 };
        String string = "23 23 80 01 4c 4c 56 31 43 52 42 31 30 48 30 30 30 30 31 36 32 01 01 a9 11 09 1a 11 12 12 0f 01 03 e8 02 00 0a 03 00 01 04 bb 05 59 f0 00 5c bc bb 59 f0 00 5f bc bb 59 07 00 5f bc bb 59 f0 00 63 bc bb 59 02 00 64 bc bb 59 f0 00 64 bc bb 59 f0 00 68 bc bb 59 40 00 68 bc bb 59 f0 00 69 bc bb 59 07 00 69 bc bb 59 f0 00 69 bc bb 59 f0 00 6a bc bb 59 f0 00 6c bc bb 59 f0 00 6d bc bb 59 02 00 6e bc bb 59 f0 00 70 bc bb 59 f0 00 72 bc bb 59 f0 00 72 bc bb 59 f0 00 73 bc bb 59 07 00 74 bc bb 59 f0 00 76 bc bb 59 f0 00 77 bc bb 59 02 00 78 bc bb 59 f0 00 7a bc bb 59 f0 00 7d bc bb 59 07 00 7e bc bb 59 f0 00 80 bc bb 59 f0 00 81 bc bb 59 02 00 81 bc bb 59 40 00 81 bc bb 59 f0 00 82 bc bb 59 06 94 bc 07 bb 05 f0 00 95 08 bc 05 03 05 04 09 9b 0a bb 59 0b 07 00 0c 9d 0d bc 0e bb 59 f0 00 9d bc bb 59 f0 00 9d bc bb 59 f0 00 9d bc bb 59 f0 00 9e bc bb 59 f0 00 9f bc bb 59 02 00 a2 bc bb 59 f0 00 a4 bc bb 59 f0 00 a5 bc bb 59 07 00 a7 bc bb 59 f0 00 a9 bc bb 59 02 00 aa bc bb 59 f0 00 ac bc bb 59 f0 00 ac bc bb 59 f0 00 ae bc bb 59 f0 00 ae bc bb 59 f0 00 af bc bb 59 07 00 b0 bc bb 59 f0 00 b2 bc bb 59 f0 00 b3 bc bb 59 02 00 b4 bc bb 59 f0 00 b8 bc bb 59 f0 00 b9 bc bb 59 07 00 ba bc bb 59 f0 00 bd bc bb 59 f0 00 bd bc bb 59 02 00 bd bc bb 59 f0 00 be bc bb 59 f0 00 c0 bc bb 59 f0 00 c2 bc bb 59 f0 00 c3 bc bb 59 07 00 c6 bc bb 59 0f 94 bc 8a";
        bytes = ByteArrayUtil.hexStringToBytes(string);
        ParamQueryReq paramQueryReq = new ParamQueryReq();
        DataDecoder<ParamQueryReq> decoder = new DataDecoder<>();
        try {
            decoder.decode(paramQueryReq, bytes);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(paramQueryReq.toString());
    }
}
